package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.constants.CommonConstant;
import com.dmcb.common.business.utils.JsonUtil;
import com.dmcb.common.business.utils.ListUtil;
import com.dmcb.common.business.utils.WebUtil;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.crawlers.article.SohuParser;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.entities.Author;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 搜狐爬取
 * Created by long on 2016/3/14.
 */
@Service
public class SohuCrawler extends BaseCrawler {

    Logger logger = LoggerFactory.getLogger(SohuCrawler.class);
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;

    @Autowired
    private SohuParser sohuParser;

    @Override
    protected String url() {
        return CrawlConstant.SOHU_URL;
    }

    @Override
    protected int tag() {
        return CrawlConstant.SOHU_TAG;
    }

    @Override
    protected String avatarSelect() {
        return "div.profile_all>img";
    }

    @Override
    protected String channelNameSelect() {
        return "p#ff";
    }

    @Override
    protected String listSelect() {
        return "div.content";
    }

    @Override
    protected String titleSelect() {
        return "div.wrap_title>a";
    }

    @Override
    protected String authorLink() {
        return "span.writer>a,h4>a";
    }

    @Override
    protected String authorSelect() {
        return "p#ff";
    }

    @Override
    protected JsonResult crawl(int authorId, String link, Author author, Document doc, boolean all) {
        // 爬取文章
        //http://mp.sohu.com/profile?xpt=Zmx5YXJyb3dAc29odS5jb20=
        //http://mp.sohu.com/apiV2/profile/newsListAjax?xpt=Zmx5YXJyb3dAc29odS5jb20=&pageNumber=4&pageSize=10&categoryId=&_=1493183533610
        int page = 1;
        int totalAll = 0;
        String key = link.substring(link.lastIndexOf("xpt=")+4, link.length());
        while (page <= CrawlConstant.CRAWL_PAGE_TOTAL) {
            String json = WebUtil.buildRequest("http://mp.sohu.com/apiV2/profile/newsListAjax?xpt=" + key + "&pageNumber=" + page + "&pageSize=10",false);
           json =  json.substring(1,json.length()-1).replace("\\","");
            page++;
            Map<String, Object> objectMaps = JsonUtil.getResult(json);
            if (objectMaps.get("data") == "[]") {
                break;
            }
            List<Map<String, Object>> mapList = (ArrayList) objectMaps.get("data");
            if (ListUtil.isEmpty(mapList)) {
                break;
            }
            JsonResult result = iterate(authorId, mapList);
            if (!result.isSuccess()) {
                return result;
            }
            totalAll += result.getInt();
        }
        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);
    }

    protected JsonResult iterate(int authorId, List<Map<String, Object>> mapList) {
        int succ = 0;
        int exist = 0;
        for (Map<String, Object> map : mapList) {
            // 获取标题、链接元素
            String title = WebUtil.URLDecode((String) map.get("title"));
            if (StringUtils.isBlank(title)) {
                continue;
            }
            try {
                // 校验作者、标题是否已存在
                Article article = articleCrawlMapper.queryByAuthorTitle(authorId, title);
                if (article != null) {
                    exist++;
                    if (exist >= 6 && succ == 0) {
                        return success(succ);
                    }
                    continue;
                }
                //www.sohu.com/a/138507533_398798
                //http://news.sohu.com/20170508/n492163567.shtml
                String href = ((String) map.get("url")).replace("\\", "");
                if (StringUtils.isBlank(href)) {
                    continue;
                }
                if(!href.contains("http")){
                    href ="http:"+href;
                }
                JsonResult result = parse(authorId, href);
                succ += result.isSuccess() ? 1 : 0;
            } catch (Exception e) {
                logger.error("Parser.parse error,{}", title, e);
                return error(CommonConstant.ABNORMAL_MSG);
            }
        }
        return success(succ);
    }


    @Override
    protected JsonResult parse(int authorId, String link) throws Exception {
        return sohuParser.parse(authorId, link);
    }

    @Override
    protected String getBody(Document doc) {
        return sohuParser.getBody(doc);
    }
}
